package edu.hebeu.evil.freshmenguide.Welcome;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import de.hdodenhof.circleimageview.CircleImageView;
import edu.hebeu.evil.freshmenguide.MainActivity;
import edu.hebeu.evil.freshmenguide.R;
import edu.hebeu.evil.freshmenguide.Utils.Utils;
import rb.popview.PopField;

/********************************************
 * 工程名：FreshmenGuide
 * 包名：edu.hebeu.evil.freshmenguide.Welcome
 * 时间：16-7-4 下午7:48
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：欢迎界面
 ********************************************/
public class Welcome extends AppCompatActivity
{
    private ImageView background;
    private CircleImageView logo;
    private TextView tag;
    private TextView version;
    private FrameLayout frameLayout;

    private PopField popField;/*logo爆炸动画*/
    private ImageView wait;

    private Bundle bundle;/*向主界面传递版本信息*/


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);

        popField = PopField.attach2Window(this);
        initView();
        initData();
       // readSharedPreferences();
    }


    /**
     * 初始化控件
     */
    private void initView()
    {
        background = (ImageView) findViewById(R.id.welcome_background);
        logo = (CircleImageView) findViewById(R.id.welcome_logo);
        tag = (TextView) findViewById(R.id.welcome_tag);
        version = (TextView) findViewById(R.id.version);
        wait = (ImageView) findViewById(R.id.welcome_wait);
        frameLayout = (FrameLayout) findViewById(R.id.welcome_frameLayout);
    }


    /**
     * 读取版本信息
     */
    private void readSharedPreferences()
    {
        SharedPreferences sharedPreferences = Utils.getSharedPreference();


        int versionCode_Old = sharedPreferences.getInt("versionCode", 0);

        int versionCode = Utils.getVersionCode();

        if (versionCode_Old < versionCode)
        {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt("versionCode", versionCode);
            editor.apply();

            bundle.putBoolean("isLead", true);
        }
        else
            bundle.putBoolean("isLead", false);
    }


    /**
     * 初始化数据
     */
    private void initData()
    {
        Utils.Glide(R.drawable.welcome_background, background);
        Utils.Glide(R.drawable.hebeu_logo, logo);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus)
    {
        if (hasFocus)
        {
            new Utils.SHandler()
            {
                @Override
                public void handler()
                {
                    Animation animation = AnimationUtils.loadAnimation(Welcome.this, R.anim.welcome);
                    frameLayout.startAnimation(animation);
                    logo.setVisibility(View.VISIBLE);
                    animation.setAnimationListener(new Animation.AnimationListener()
                    {
                        @Override
                        public void onAnimationStart(Animation animation)
                        {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation)
                        {
                            popField.popView(logo);
                            Animation anim = new AlphaAnimation(0, 100);
                            anim.setDuration(1000);
                            Utils.Glide(R.drawable.watingforyou, wait);
                            wait.startAnimation(anim);
                            wait.setVisibility(View.VISIBLE);
                            new Utils.SHandler()
                            {
                                @Override
                                public void handler()
                                {
                                    startActivity(new Intent(Welcome.this, MainActivity.class)
                                            .putExtra("Bundle", bundle));
                                    finish();
                                }
                            }.start(2000);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation)
                        {
                        }
                    });
                }
            }.start(1000);
        }
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if (popField.isActivated())
            popField.clear();
    }
}
